const WXAPI = require('../../wxapi/main')
const CONFIG = require('../../config.js')

var t = require("../../nitro.js"), e = (require("../../configs.js"), getApp());
const AUTH = require('../../utils/auth')
const TOOLS = require('../../utils/tools.js')


Page({
  data: {
    isIpx: e.globalData.isIpx,
    stores: [],
    kanjiaList: [],
    kj_top:wx.getStorageSync('kj_top'),
    kj_bjcolor:wx.getStorageSync('kj_bjcolor'),
    kj_btcolor:wx.getStorageSync('kj_btcolor'),
  },
  onLoad: function (t) {

    wx.setNavigationBarColor({
      frontColor: '#ffffff', // 必写项
      backgroundColor: wx.getStorageSync('kj_bjcolor'), // 必写项
    })

    this.setData({
      options: t,
      kj_top:wx.getStorageSync('kj_top'),
      kj_btcolor:wx.getStorageSync('kj_btcolor')
    });


    wx.showShareMenu({
      withShareTicket: true,
    })
    const that = this
    // 读取分享链接中的邀请人编号
    if (e && e.inviter_id) {
      wx.setStorageSync('referrer', e.inviter_id)
    }
    // 读取小程序码中的邀请人编号
    if (e && e.scene) {
      const scene = decodeURIComponent(e.scene)
      if (scene) {
        wx.setStorageSync('referrer', scene.substring(11))
      }
    }
    // 静默式授权注册/登陆
    AUTH.checkHasLogined().then(isLogined => {
      if (!isLogined) {
        AUTH.authorize().then(aaa => {
          if (CONFIG.bindSeller) {
            AUTH.bindSeller()
          }
          // TOOLS.showTabBarBadge()
        })
      } else {
        if (CONFIG.bindSeller) {
          AUTH.bindSeller()
        }
        // TOOLS.showTabBarBadge()
      }
    })


    that.kanjiaGoods()





  },
  onReady: function () { },

  // 分享
  onShareAppMessage: function () {
    return {
      title: "秒秒哆儿童摄影",
    };
  },
  //用户点击右上角分享朋友圈
  onShareTimeline: function () {
    return {
      title: "秒秒哆儿童摄影",
    }
  },

  // 跳转
  toDetailsTap: function(e) {
    console.log(e);
    const id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: `/pages/kj/index?id=${id}`,
      })
  },

  // 获取砍价商品
  async kanjiaGoods() {
    // https://www.yuque.com/apifm/nu0f75/wg5t98
    const res = await WXAPI.goodsv2({
      kanjia: true
    });
    if (res.code == 0) {
      const kanjiaGoodsIds = []
      res.data.result.forEach(ele => {
        kanjiaGoodsIds.push(ele.id)
      })
      const goodsKanjiaSetRes = await WXAPI.kanjiaSet(kanjiaGoodsIds.join())
      if (goodsKanjiaSetRes.code == 0) {
        res.data.result.forEach(ele => {
          const _process = goodsKanjiaSetRes.data.find(_set => {
            return _set.goodsId == ele.id
          })
          if (_process) {
            ele.process = 100 * _process.numberBuy / _process.number
            ele.process = ele.process.toFixed(0)
          }
        })
        this.setData({
          kanjiaList: res.data.result
        })
      }
    }
  },
  onShow: function () {

    let that = this;
    that.kanjiaGoods();
    // var a = this, o = t.getStore();

    // wx.setNavigationBarTitle({
    //     title: "" + o.name
    // }),

    // delete e.globalData.categories, delete e.globalData.items, delete e.globalData.themeGroups;







    // WXAPI.goodsCategory().then(function(res) {
    //     let categories = [];
    //     if (res.code == 0) {
    //       categories = categories.concat(res.data);
    //       console.log(categories);


    //       that.setData({
    //         loaded: !0,
    //         items: categories,
    //       });

    //     }


    //   })



  },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () { },
  initStores: function () { },
  // 点击跳转
  itemTapHandler: function (a) {


    wx.setStorageSync("jxzmd", a.currentTarget.dataset.id)

    var r = "/pages/artworks/artworks?dianpu=" + a.currentTarget.dataset.id;
    wx.navigateTo({
      url: r,
      success: function (t) { },
      fail: function (t) { },
      complete: function (t) { }
    });


    // var o = a.currentTarget.dataset.storeid,
    //     i = a.currentTarget.dataset.type,
    //     n = this.data.stores.find(function(t) {
    //     return t.id === o;
    // });

    // t.setStore(o), e.globalData.cart.store = n, e.globalData.cart.storeId = o;

    // // var r = "/pages/artworks/artworks?store_id=" + o + "&type=" + i;

    // var r = "/pages/artworks/artworks?category_id=329418&type=" + i;

    // if("wj" === i){
    //     r = "/pages/product-detail/product-detail?category_id=329417&type=" + i
    // }


    // wx.navigateTo({
    //     url: r,
    //     success: function(t) {},
    //     fail: function(t) {},
    //     complete: function(t) {}
    // });
  }

});